package com.dqhl.business.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dqhl.business.R;
import com.dqhl.business.modle.Order;
import com.dqhl.business.util.ViewHolder;

import java.util.List;

/**
 * Created by guanluocang on 2016/11/3.
 */
public class OrderAdapter extends BaseAdapter {

    private Context context;
    private List<Order> orderList;
    private LayoutInflater inflater;
    public OnDeliverListener mOnDeliverListener;

    public OrderAdapter(Context context, List<Order> orderList, OnDeliverListener onDeliverListener) {
        this.context = context;
        this.orderList = orderList;
        this.mOnDeliverListener = onDeliverListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return orderList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.layout_order_item, viewGroup, false);
        }

        TextView tv_order_num = ViewHolder.get(view, R.id.tv_order_num);
        TextView tv_goodsOwner = ViewHolder.get(view, R.id.tv_goodsOwner);
        TextView tv_goodsName = ViewHolder.get(view, R.id.tv_goodsName);
        TextView tv_goodsAdd = ViewHolder.get(view, R.id.tv_goodsAdd);
        LinearLayout ll_send = ViewHolder.get(view, R.id.ll_send); //待发货
        LinearLayout ll_sign = ViewHolder.get(view, R.id.ll_sign); //待签收
        LinearLayout ll_wait = ViewHolder.get(view, R.id.ll_wait); //待收货
        LinearLayout ll_accepted = ViewHolder.get(view, R.id.ll_accepted); //已收到货
        ImageView iv_send_goods = ViewHolder.get(view, R.id.iv_send_goods);
        ImageView iv_sign_goods = ViewHolder.get(view, R.id.iv_sign_goods);

        Order order = orderList.get(i);
        tv_order_num.setText(order.getCode());
        tv_goodsOwner.setText(order.getUser_name());
        tv_goodsName.setText(order.getGoods());
        tv_goodsAdd.setText(order.getAddress());
        String orderState = order.getOrder_state();
        String sellerState = order.getSeller_state();
        if ("1".equals(orderState) && "1".equals(sellerState)) {//待发货
            ll_send.setVisibility(View.VISIBLE);
            ll_sign.setVisibility(View.GONE);
            ll_wait.setVisibility(View.GONE);
            ll_accepted.setVisibility(View.GONE);
        }
        if ("2".equals(orderState) && "1".equals(sellerState)) {//待签收
            ll_send.setVisibility(View.GONE);
            ll_sign.setVisibility(View.VISIBLE);
            ll_wait.setVisibility(View.GONE);
            ll_accepted.setVisibility(View.GONE);
            iv_sign_goods.setVisibility(View.GONE);
        }
        if ("2".equals(orderState) && "2".equals(sellerState)) {//待收货
            ll_send.setVisibility(View.GONE);
            ll_sign.setVisibility(View.GONE);
            ll_wait.setVisibility(View.VISIBLE);
            ll_accepted.setVisibility(View.GONE);
        }
        if ("2".equals(orderState) && "3".equals(sellerState)) {//已收到货
            ll_send.setVisibility(View.GONE);
            ll_sign.setVisibility(View.GONE);
            ll_wait.setVisibility(View.GONE);
            ll_accepted.setVisibility(View.VISIBLE);
        }

        iv_send_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnDeliverListener.onDeliever(i);
            }
        });
//        iv_sign_goods.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mOnDeliverListener.onDeliever(i);
//            }
//        });

        return view;
    }

    public interface OnDeliverListener {
        void onDeliever(int position);
    }
}
